package Server;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Trung
 */
import java.net.*;
import java.io.*;        
public class TCPServer2 {
    public static void main(String arg[])
    {
        try
        {
            ServerSocket ss = new ServerSocket(3001);
            System.out.println("Server waiting client: ");   
            
            
            Socket sk = ss.accept();
            //System.out.println("InetAddress: " + ss.getInetAddress());
            //System.out.println("LocalSocketAddress" + ss.getLocalSocketAddress());
            System.out.println(sk.getPort());
                       
            
            InputStream is = sk.getInputStream();
            OutputStream os = sk.getOutputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(os));
            String rec = "";
            String send = "";
            do
            {                               
                rec = br.readLine();
                if(rec != "")
                    System.out.println("Receiveed: " + rec);
                
                if(rec.equalsIgnoreCase("quit"))
                    break;
                
                BufferedReader bufR = new BufferedReader(new InputStreamReader(System.in));
                send = bufR.readLine();
                if(send != "")
                    bw.write(send);
                bw.newLine();    
                bw.flush();
            }
            while(true);
            bw.close();
            br.close();
            
                      
            
        }
        catch(IOException ex)
        {        
            System.out.println(ex.getMessage());
        }
    }
    
    
}
